package com.example.exp_20212294;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;

import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private DrawingBoard mDrawingBoard;
    private Slider mSlider;
    //画板颜色
    private ImageView mColor;
    //对画板的操作
    private ImageView mPaint;
    private ImageView mEraser;
    private ImageView mClean;
    private ImageView mLast;
    private ImageView mNext;
    //记录画笔大小
    private float size;


    //获取像素点
    private int dip2x(float depValue){
        final float density = getResources().getDisplayMetrics().density;
        return (int)(depValue*density+0.5f);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        initEvent();
        addSliderListener();

        mColor=findViewById(R.id.iv_color);
        registerForContextMenu(mColor);//注册上下文菜单
    }

    //重写上下文菜单的创建方法
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo) {
        MenuInflater inflator = new MenuInflater(this);
        inflator.inflate(R.menu.menu_context, menu);
        super.onCreateContextMenu(menu, v, menuInfo);
        //这个方法进行的是长按
    }

    //上下文菜单的点击事件
    @Override
    public boolean onContextItemSelected(@NonNull MenuItem item) {
        if (item.getItemId()==R.id.blue)
            mDrawingBoard.setmPaintColor(Color.BLUE);
        else if (item.getItemId()==R.id.green)
            mDrawingBoard.setmPaintColor(Color.GREEN);
        else if (item.getItemId()==R.id.pink)
            mDrawingBoard.setmPaintColor(Color.RED);
        else if (item.getItemId()==R.id.yellow)
            mDrawingBoard.setmPaintColor(Color.YELLOW);
        else if (item.getItemId()==R.id.gray)
            mDrawingBoard.setmPaintColor(Color.GRAY);
        else if (item.getItemId()==R.id.black)
            mDrawingBoard.setmPaintColor(Color.BLACK);
        return super.onContextItemSelected(item);
    }

    @Override
    protected void onResume() {
        super.onResume();
        //默认画笔大小
        size  = dip2x(10);
    }

    private void initView(){
        mDrawingBoard = findViewById(R.id.draw_board);
        mSlider = findViewById(R.id.slider);
        mPaint = findViewById(R.id.iv_paint);
        mEraser = findViewById(R.id.iv_eraser);
        mClean = findViewById(R.id.iv_clean);
        mLast = findViewById(R.id.iv_last);
        mNext = findViewById(R.id.iv_next);
    }
    //实现滑动小圆点改变画笔线条粗细大小
    private void addSliderListener(){
        mSlider.addListener(new Slider.OnSliderChangedListener() {
            @Override
            public void positionChanged(float p) {
                if (size > 0) {
                    mDrawingBoard.setmPaintSize((int) (p * size * 2));
                }
            }
        });
    }

    private void initEvent(){

        //设置默认画笔背景为蓝色
        mPaint.getBackground().setLevel(1);
        mPaint.getDrawable().setLevel(1);
        mPaint.setOnClickListener(this);
        mEraser.setOnClickListener(this);
        mClean.setOnClickListener(this);
        mLast.setOnClickListener(this);
        mNext.setOnClickListener(this);

    }

    //设置画板清空对话框
    private void alertDialogClean(){
        AlertDialog.Builder builder = new AlertDialog.Builder(this);
        builder.setMessage("确定要请空画板吗？");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {
                mDrawingBoard.clean();
            }
        });
        builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
            @Override
            public void onClick(DialogInterface dialogInterface, int i) {

            }
        });
        final  AlertDialog dialog = builder.show();
        dialog.show();
    }

    @Override
    public void onClick(View view) {
        if(view.getId()==R.id.iv_paint) {
            if (mDrawingBoard.getMode() != DrawMode.PaintMode) {
                mDrawingBoard.setMode(DrawMode.PaintMode);
            }
            mPaint.getDrawable().setLevel(1);
            mPaint.getBackground().setLevel(1);
            mEraser.getDrawable().setLevel(0);
            mEraser.getBackground().setLevel(0);
        }
        else if(view.getId()==R.id.iv_eraser) {
            if (mDrawingBoard.getMode() != DrawMode.EraserMode) {
                mDrawingBoard.setMode(DrawMode.EraserMode);
            }
            mPaint.getDrawable().setLevel(0);
            mPaint.getBackground().setLevel(0);
            mEraser.getDrawable().setLevel(1);
            mEraser.getBackground().setLevel(1);
        }
        else if(view.getId()==R.id.iv_clean) {
            alertDialogClean();
        }
        else if(view.getId()==R.id.iv_last) {
            mDrawingBoard.lastStep();
        }
        else if(view.getId()==R.id.iv_next) {
            mDrawingBoard.nextStep();
        }


    }
}

